import server from '@/utils/server'
import ServerConstants from '@/utils/ServerConstants'
import {encode} from "js-base64/base64"


export function saveCart (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_CART_SAVE, {local:encode(JSON.stringify(params))},
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}


export function getCart (success, errorfun) {
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_CART_GET, {},
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}



export function delCart (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_CART_DEL, params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

export function delCartIndex (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_CART_DEL_INDEX, params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}
